package jupiter.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import java.io.Serializable;
import java.util.Properties;

import javax.jms.*;


import jupiter.servlets.MessageEventListener;

import org.apache.activemq.command.*;
import org.apache.activemq.util.ByteSequence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;



public class EventSubscriber
{
	Logger logger = LoggerFactory.getLogger(EventSubscriber.class);
	private TopicConnectionFactory connectionFactory;
    private TopicConnection connection;
    private TopicSession session;
	//private TopicPublisher eventsPublisher;
	private TopicPublisher messagesPublisher;
    public EventSubscriber(){
        //System.out.println("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");    
    	InitialContext ic;
			try {
				ic = new InitialContext();
	            Object o = ic.lookup("java:comp/env/jms/connectionFactory");
	            connectionFactory = (TopicConnectionFactory)o;
	            Topic messagesTopic = (Topic)ic.lookup("java:comp/env/jms/messages");
	            connection = connectionFactory.createTopicConnection();
	            session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
	            TopicSubscriber messagesSubscriber = session.createSubscriber(messagesTopic);
	            messagesSubscriber.setMessageListener(new MessageEventListener());
	            messagesPublisher = session.createPublisher(messagesTopic);
	            messagesPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
	            connection.start();
	            String path = getClass().getProtectionDomain().getCodeSource().
	                          getLocation().toString().substring(5);
	            java.io.FileInputStream fis;
				try {
					fis = new java.io.FileInputStream
					   (new java.io.File( path + "../../src/main/config/appconfig.props"));
					Application.props.load(fis);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (NamingException e) {
	            logger.error("NamingException", e);
			} catch (JMSException e) {
	            logger.error("JMSException", e);
			}
	}
    public void sendText(String mess){
		ActiveMQTextMessage m = new ActiveMQTextMessage();
		try {
			m.setText(mess);
			messagesPublisher.publish(m);
		} catch (MessageNotWriteableException e) {
            logger.error("MessageNotWriteableException", e);
		} catch (JMSException e) {
            logger.error("JMSException", e);
		}
    }


    public void sendMessage(String mess){
    	try{
    		BytesMessage bm = session.createBytesMessage();
    		bm.writeObject(mess.getBytes());
    		messagesPublisher.publish(bm);
		} catch (JMSException e) {
            logger.error("JMSException", e);
		}
    	
    }

}